SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[FundPropBreakoutPCT]

@FundID VARCHAR(10)
,@NAVDate DATE

 AS
BEGIN

	
	/*Dbg
	SET @FundID = 'FSIC'
	*/

	DECLARE @ExportId INT = (SELECT MAX(e.ExportId) FROM FSFundModel.Reporting.Export e WHERE e.FundId =@FundId AND e.NAVDate = @NAVDate)
	DECLARE @CalcMV DECIMAL(38,10) = (SELECT SUM(a.MktVal) FROM FSFundModel.Reporting.Asset a WHERE a.ExportId = @ExportId AND a.IsActiveEndOfDay = 1) 
	 SELECT     
				ISNULL(ah.[NBSReason], 'Broadly Syndicated / Other') AS PropClass, 
				CAST((SUM(a.MktVal)*1000 ) / MAX(@CalcMV) AS DECIMAL (38,10))/1000 AS PctFund

	
	 FROM       [FSFundModel].[Reporting].Asset a
	            INNER JOIN  ReportingScripts.live.AssetHelper(@ExportId) ah ON ah.AssetIntrnlId = a.AssetIntrnlId
				INNER JOIN [FSFundModel].[Reporting].Instrument i
	            ON         i.ExportId     = a.ExportId
	            AND        a.InstrumentId = i.InstrumentId

	
				--LEFT JOIN  [FSFundModel].[Reporting].FundCreditInstrument fci 
				--ON a.ExportId = fci.ExportId AND i.InstrumentId = fci.InstrumentId
	            
				--INNER JOIN [FSFundModel].[Reporting].Fund f
	   --         ON         a.ExportId = f.ExportId


	 WHERE      a.ExportId                =@ExportId
	 AND a.IsActiveEndOfDay = 1
	           
	GROUP BY ah.[NBSReason]
END

GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Live/CharlieWaterman', 'SCHEMA', N'dbo', 'PROCEDURE', N'FundPropBreakoutPCT', NULL, NULL
GO
